Improve function to be called from a session and a window file