cleaned up some code; fixed a bug