Code cleanup + minor fixes