Improved some code and fixed an issue