Removed all the unused parts from the previous implementation and refactored many checks to avoid duplication.