Corrected tag changing and frame writing. Some code refactoring.