Horrible code. Works but needs to be improved