Updated code with bug fixes.