Code cleanup and added flexibility