add reverse functionallity and refactor code