fixed lots of bugs, and restructure layout