Improved encapsulation and fixed bug