fixed memory leak with FIELD_FREE

Fixed important memory leak that happened when FIELD_FREE was selected.
Also did a general cleanup of used code.