fixed some code, still a bit messy