cleaned up code somewhat