fixed #1, simplified code