organized and cleaned up code