Refractored to have have less erros. Not dumbproof yet