Add portfolio functionality, fix a number of problems related to portfolio