Removed duplicate code, splitted methods into smaller parts