fixed distance formula and improved side switching functionality