moved some code around, and cleaned up some redundancies