refactored, cleaned up code