ugly as hell but working