Cleanup of code and fixing of some bugs for deployment