added working code, sans comments