Moved code out of contructor to avoid memory leak