general cleanup and updated imagedata handling