more code cleanup + fix #45, fix #48, fix #49