Converted code to pure JS