Code for auto leveling