Very ugly; might need rewrite