fixed code based on your comments