fix various bugs and improve security