cleaned up some code. ready for further improvement