Full code cleanup and a few feature additions