cleaned up code, commands sorted into logical order