Removed a lot of duplicated code