Improved code, mostly for readability