major rewrite needed, so much spaghetti code