Made the function have better performance and less memory usage, and added a length getter/setter