Changed malloc and free to be configurable by the user; minor tweaks.