Fixed rewriting problems. Added id check