cleaned up the code somewhat