refactured my code