heavy refactoring and code clean up. moved code around a lot. closing issue 3