major cleanup. this is starting to look like not-spaghetti code now :)