Fixed problem with setTimeout() and improved for security puproses