fixing timeout bug, improving js readability