fix it and organize some code