Fixed code as suggested in comments