Started with PR 374 (which is for master) and merged it to development branch, attempting to resolve all merge conflicts

I didn't alter the code formatting, trying to make sure this is as close to the original as possible, except rebased for development.