Some small patches from Aidil to fix a runtime error and some code cleanup.