[misc] Added tests, fixed minor code issues uncovered by tests