Added debugging and some reformation of code.