refactored a bit and cleaned up code