Code cleanup + fix small bug