fixed the seg fault, function not working properly
Miscellaneous bug fixes (but it still doesn't work).