Refacgtored some code