Refactored more code to get rid of duplicated code