lots of code cleanup and making things work