Code cleanup & a bug fix