refactored JS and some C# code