LPS-33812 Fixed some bugs and cleaned up code