Code improvements + bug fixes