Refined a lot of code