more bugfixes
added bad arg check, reference cleanup to reduce memory usage