Fixed runtime bug and use the alarm API instead of setTimeout
Fix for optimization