massively cleaned up code and added functionality