Cleaning up the code significnatly.