Further cleaning of the code. Still messy.