needs some cleanup and tweaking