refactored and clean up code