added leveling logic