Fixed some potential bugs and optimized some code