Fixed code based on comments