rewrite this in a saner way