Updated code, should be bugfree(tm)