Code cleanup, bug fix