Removed duplicate code causing bugs