this code is getting pretty rough... but it works