Some code cleanup and fix of csrf usage